//重写webpack配置
import path from 'path'
import CompressionPlugin from 'compression-webpack-plugin'
//配置路径解析
function Resolve(url: string): string {
    return path.resolve(__dirname, url)
}

//导出配置
module.exports = {
    //重写webpack
    webpack: {
        alias: {
            '@': Resolve("./src"),
            "@assets": Resolve("./src/assets"),
            "@utils": Resolve("./src/utils"),
        },
        configure: (webpackConfig: any, { env, paths }: any) => {
            //情景配置
            if (env == "production") {
                //打包时公共路径修改
                webpackConfig.output.publicPath = "./";
                //修改打包的目录名称
                webpackConfig.output.path = Resolve("./dist");//修改dist
                paths.appBuild = Resolve("./dist");//修改dist
            }
            return webpackConfig;
        },
        plugins: {
            //配置插件
            add: [
                new CompressionPlugin({
                    filename: '[path][base].gz', // 目标资源名称。[file] 会被替换成原资源。[path] 会被替换成原资源路径，[query] 替换成原查询字符串
                    algorithm: 'gzip', // 算法       
                    test: new RegExp('\\.(js|css)$'), // 压缩 js 与 css
                    threshold: 10240, // 只处理比这个值大的资源。按字节计算
                    minRatio: 0.8 // 只有压缩率比这个值小的资源才会被处理
                })
            ]
        }
    },
    devServer: (defaultDevServer: any, { env }: any) => {
        defaultDevServer.proxy = {
            [process.env.REACT_APP_BASEURL!]: {
                target: process.env.REACT_APP_NETWORK,//代理地址
                changeOrigin: true,//覆盖主机的来源
                pathRewrite: { [`^${process.env.REACT_APP_BASEURL}`]: '' },//替换
            },
        }
        return defaultDevServer;
    }
}